package com.yifeng.repo.controller.excel.configure;

import com.yifeng.repo.base.utils.redis.worker.RedisCacheWorker;
import com.yifeng.repo.base.utils.redis.worker.RedisLockWorker;
import com.yifeng.repo.controller.excel.loader.ExcelLoaderManager;
import com.yifeng.repo.controller.excel.loader.worker.ExcelLoaderOssWorker;
import com.yifeng.repo.controller.excel.loader.worker.ExcelLoaderRedisWorker;
import com.yifeng.repo.controller.excel.loader.worker.LsdWorker;
import com.yifeng.repo.controller.excel.simple.download.DownloadReturnValueHandler;
import com.yifeng.repo.controller.excel.simple.upload.UploadArgumentResolver;
import com.yifeng.repo.controller.excel.web.ExcelLoaderServletRegistrar;
import com.yifeng.repo.tookit.oss.OssFileClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by daibing on 2022/12/29.
 */
@Configuration
@EnableConfigurationProperties(ExcelLoaderProperties.class)
@ConditionalOnClass({ExcelLoaderProperties.class})
public class ExcelLoaderConfiguration implements WebMvcConfigurer {
    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private ExcelLoaderProperties properties;

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.controller.excel.loader", name = "enable-loader-manager")
    public ExcelLoaderRedisWorker excelLoaderRedisWorker(RedisCacheWorker redisCacheWorker, RedisLockWorker redisLockWorker) {
        return new ExcelLoaderRedisWorker(applicationName, properties, redisCacheWorker, redisLockWorker);
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.controller.excel.loader", name = "enable-loader-manager")
    public ExcelLoaderOssWorker excelLoaderOssWorker(OssFileClient ossFileClient) {
        return new ExcelLoaderOssWorker(ossFileClient);
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.controller.excel.loader", name = "enable-decrypt-file")
    public LsdWorker lsdWorker() {
        return new LsdWorker(properties.getLsdConsoleParam());
    }

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = "yfcloud.controller.excel.loader", name = "enable-loader-manager")
    public ExcelLoaderManager excelLoaderManager(ExcelLoaderRedisWorker excelLoaderRedisWorker,
                                                 ExcelLoaderOssWorker excelLoaderOssWorker,
                                                 @Autowired(required = false) LsdWorker lsdWorker) {
        return new ExcelLoaderManager(properties, excelLoaderRedisWorker, excelLoaderOssWorker, lsdWorker);
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.controller.excel.loader", name = "enable-loader-manager")
    public ExcelLoaderServletRegistrar excelLoaderServletRegistrar(ExcelLoaderManager excelLoaderManager) {
        return new ExcelLoaderServletRegistrar(properties.getDataTaskUriPrefix(), excelLoaderManager);
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        if (properties.isEnableSimpleExport()) {
            handlers.add(new DownloadReturnValueHandler());
        }
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        if (properties.isEnableSimpleImport()) {
            resolvers.add(new UploadArgumentResolver());
        }
    }

}
